{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import torch"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(torch.cuda.is_available())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- hf-mirror.com!\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'\n",
    "print(\"--- hf-mirror.com!\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 下载"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- cache_dir: /visrag/models\n",
      "--- model_name: bert-base-uncased\n"
     ]
    }
   ],
   "source": [
    "from huggingface_hub import snapshot_download\n",
    "\n",
    "# model_name = \"qwen/Qwen2-VL-7B-Instruct\"\n",
    "model_name = \"bert-base-uncased\"\n",
    "\n",
    "# 定义目标文件夹路径\n",
    "cache_dir = os.path.join(\"/visrag\", \"models\")\n",
    "os.makedirs(cache_dir, exist_ok=True)\n",
    "\n",
    "print(f'--- cache_dir: {cache_dir}')\n",
    "print(f'--- model_name: {model_name}')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- downloading model! model_name: bert-base-uncased\n"
     ]
    },
    {
     "data": {
      "text/plain": "Fetching 16 files:   0%|          | 0/16 [00:00<?, ?it/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "51778f95081d426895d7f7b15cc02f7c"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "README.md: 0.00B [00:00, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "454e28b05c764a9dbfe875aefa063dbe"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "config.json:   0%|          | 0.00/334 [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "010cb509b48446fb997b6d1a15080cb5"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": ".gitattributes:   0%|          | 0.00/491 [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "c563e2fa53024205b5e4890f4f33ac61"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "LICENSE: 0.00B [00:00, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "385fb63babe44b0fb6d547707b1bdafb"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "(…)kage/Data/com.apple.CoreML/model.mlmodel:   0%|          | 0.00/165k [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "13b560cedf7746b690ad5caf01879b8b"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "(…)sk/float32_model.mlpackage/Manifest.json:   0%|          | 0.00/277 [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "5eb1f88822b749c6af8bc39a30c0f414"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "flax_model.msgpack:   0%|          | 0.00/438M [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "149810722aaa40618136d2b3fd936a3e"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "weight.bin:   0%|          | 0.00/532M [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "829c3df403544067ae23eb96b51dd234"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "tokenizer.json: 0.00B [00:00, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "d7acc06b876b412e9d4ddbd0042d86c1"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "model.onnx:   0%|          | 0.00/532M [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "67102170dfe0428caf234da1f7470481"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "model.safetensors:   0%|          | 0.00/440M [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "c7e7b45b03f54e099f1931a47dc0a25d"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "rust_model.ot:   0%|          | 0.00/534M [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "759cbac4359b468799debcb7609c0599"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "pytorch_model.bin:   0%|          | 0.00/440M [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "b312d43c79364089b3d5aab6d1f6d7a4"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "tf_model.h5:   0%|          | 0.00/536M [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "1c0150bb4b18468f9d58e59ef9a7c98a"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "tokenizer_config.json:   0%|          | 0.00/48.0 [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "f729c43e5c334e28bd483e31946e0d51"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "vocab.txt: 0.00B [00:00, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "90306befcfe843cdb67043f0d43c71bd"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Error while downloading from https://cdn-lfs.hf-mirror.com/bert-base-uncased/a7a17d6d844b5de815ccab5f42cad6d24496db3850a2a43d8258221018ce87d2?response-content-disposition=inline%3B+filename*%3DUTF-8%27%27tf_model.h5%3B+filename%3D%22tf_model.h5%22%3B&Expires=1731317039&Policy=eyJTdGF0ZW1lbnQiOlt7IkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTczMTMxNzAzOX19LCJSZXNvdXJjZSI6Imh0dHBzOi8vY2RuLWxmcy5oZi5jby9iZXJ0LWJhc2UtdW5jYXNlZC9hN2ExN2Q2ZDg0NGI1ZGU4MTVjY2FiNWY0MmNhZDZkMjQ0OTZkYjM4NTBhMmE0M2Q4MjU4MjIxMDE4Y2U4N2QyP3Jlc3BvbnNlLWNvbnRlbnQtZGlzcG9zaXRpb249KiJ9XX0_&Signature=F0k1WxEx0k1ISv71NA1SS3N-6AZy3Rhmu%7E95shlb1V93lyEoummEYnj2H2bWlHOeRXljX5k-LAObNXI5skWP1jslb4ev72QmuNfNLU1vbJCuFu8DZEE0qFwFJQj%7E-BuQOKAadmBXo%7EfferRTyx5C7Puy5-eOeqRjFM35ZUANoT2msKuVIdz69cUkKEnySkwThG%7E6aP4pLoLTDqGDAPoHD4rzM833y25-Vc-6lW8uFldZO2nRfo5eD-O5W7uKQjleBFIMvnsgKMM2NK14VmRZmn%7E5bUgCvkXY1vhSTL98j5Ob7Inm4y2SNM0Jchr8mXDn6XWamB0YWqnrW5fZa5Igiw__&Key-Pair-Id=K3RPWS32NSSJCE: HTTPSConnectionPool(host='cdn-lfs.hf-mirror.com', port=443): Read timed out.\n",
      "Trying to resume download...\n"
     ]
    },
    {
     "data": {
      "text/plain": "tf_model.h5:   4%|3         | 21.0M/536M [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "0ce8677c1549482cbdab43156dd5160d"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Error while downloading from https://cdn-lfs.hf-mirror.com/bert-base-uncased/afd9aa425fd45c5655d3d43a0d041f9b76729bf475d6c017a0e9304a38f89972?response-content-disposition=inline%3B+filename*%3DUTF-8%27%27rust_model.ot%3B+filename%3D%22rust_model.ot%22%3B&Expires=1731319590&Policy=eyJTdGF0ZW1lbnQiOlt7IkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTczMTMxOTU5MH19LCJSZXNvdXJjZSI6Imh0dHBzOi8vY2RuLWxmcy5oZi5jby9iZXJ0LWJhc2UtdW5jYXNlZC9hZmQ5YWE0MjVmZDQ1YzU2NTVkM2Q0M2EwZDA0MWY5Yjc2NzI5YmY0NzVkNmMwMTdhMGU5MzA0YTM4Zjg5OTcyP3Jlc3BvbnNlLWNvbnRlbnQtZGlzcG9zaXRpb249KiJ9XX0_&Signature=qQiOtI0VXvSe0EWYBuctrOixsbAXdPvqkEEnI-IWNyPM%7EBpVeRc2PR9cyS%7EHnlptwL9pCnEjW4q7y4trhp4c2%7E0DX9ZQUUMFWHkyJsikntUWuioa2igQxQFJ6639CKmomd0In7-ENgLhnHkk5nBnWWni9q1SJiVRYItKsZZsqqdYRBBqDY7w9LIsy-zdKdJ4TabSJpsEeI3FCtkvbdnfVXiL7iP8E3sMgjF8YA69oEH5D-KlxfWjW3W5azRTakRIezPDKwcjR6CFLN370LRurGOZ1RPknFbuQYNz%7EJblK6caw6oTYAXCjiyhZF-UqSXETYaQi1ujF9LhtUepPTmsSw__&Key-Pair-Id=K3RPWS32NSSJCE: HTTPSConnectionPool(host='cdn-lfs.hf-mirror.com', port=443): Read timed out.\n",
      "Trying to resume download...\n"
     ]
    },
    {
     "data": {
      "text/plain": "rust_model.ot:  57%|#####6    | 304M/534M [00:00<?, ?B/s]",
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "b9d28f3acbf949809fcf7490a409cf6b"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*** 模型已下载到: /visrag/models/models--bert-base-uncased/snapshots/86b5e0934494bd15c9632b12f734a8a67f723594\n"
     ]
    }
   ],
   "source": [
    "print(f'--- downloading model! model_name: {model_name}')\n",
    "\n",
    "# 下载模型到指定的文件夹\n",
    "model_dir = snapshot_download(model_name, cache_dir=cache_dir)\n",
    "\n",
    "print(f\"*** 模型已下载到: {model_dir}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/anaconda3/envs/VisRAG/lib/python3.10/site-packages/huggingface_hub/file_download.py:797: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n",
      "  warnings.warn(\n",
      "/root/anaconda3/envs/VisRAG/lib/python3.10/site-packages/huggingface_hub/file_download.py:797: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型和分词器已下载到cache_dir: /visrag/models\n"
     ]
    }
   ],
   "source": [
    "\n",
    "from transformers import AutoModel, AutoTokenizer\n",
    "\n",
    "# # 下载模型和分词器到指定的文件夹\n",
    "model = AutoModel.from_pretrained(model_name, cache_dir=cache_dir)\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=cache_dir)\n",
    "\n",
    "print(f\"模型和分词器已下载到cache_dir: {cache_dir}\")\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}